///////////////////////////////////////////////////////////////////////////////
//
// lighthouse3D - 3D Software Library
//
// File: C2DScreen.cpp
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-2013 Marcus Reinhardt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
///////////////////////////////////////////////////////////////////////////////
//
// You can find a copy of the GNU General Public License (gpl.txt) in the
// directory of the library.
//
///////////////////////////////////////////////////////////////////////////////
#include "C2DScreen.h"

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::C2DScreen( ... )
///////////////////////////////////////////////////////////////////////////////
C2DScreen::C2DScreen()
{
  sizeX = 0;
  sizeY = 0;

  useBColor = true;
  bcolor = _2DScreenClear_;

  fontReady = false;

  fontColor = 255;

  guiTransparency = 0.75;

  lastAssignedHandle = 0;

  for (int i=0; i<_GUIHandles_; i++)
  {
      managedGUIHandles[i].x = 0;
      managedGUIHandles[i].y = 0;
      managedGUIHandles[i].w = 1;
      managedGUIHandles[i].h = 1;
      managedGUIHandles[i].visible = true;
      managedGUIHandles[i].type = _GUITypeUnknown_;
      managedGUIHandles[i].parent = NULL;
  }

  MainHandle = getGUIHandle();
  if (MainHandle != NULL)
  {
      strcpy(MainHandle->caption, "The Main Screen");
      MainHandle->x = 0;
      MainHandle->y = 0;	   
      MainHandle->w = 1;	   
      MainHandle->h = 1;
      MainHandle->type = _GUITypeMainScreen_;
      MainHandle->parent = NULL;
  }

  cachedY = -1;
  cachedSizeX = -1;
  cachedYPos = 0;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::~C2DScreen( ... )
///////////////////////////////////////////////////////////////////////////////
C2DScreen::~C2DScreen()
{
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::getScreenHandle( ... )
///////////////////////////////////////////////////////////////////////////////
T2DData *C2DScreen::getScreenHandle()
{
  return &d;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setPixel( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::setPixel(int x, int y, unsigned int c)
{
  if ( (y < 0) || (y>=sizeY) )
    return;
  if ( (x < 0) || (x>=sizeX) )
    return;

  d.d[y*sizeX+x] = c;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setPixelFast( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::setPixelFast(int x, int y, unsigned int c)
{
  // This is to reduce the multiplications
  if ( (sizeX != cachedSizeX) && (y != cachedY) ) {
	cachedYPos = y*sizeX;
  }
  d.d[cachedYPos+x] = c;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setPixelTransparent( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::setPixelTransparent(int x, int y, unsigned int c)
{
  if (guiTransparency != 1) {
	TRGBColorTriple tRGB1, tRGB2, tRGBn;
    float faktor = guiTransparency;

    if ( (y < 0) || (y>=sizeY) )
        return;
    if ( (x < 0) || (x>=sizeX) )
        return;

	tRGB1 = getRGBColor(getPixel(x,y));
	tRGB2 = getRGBColor(c);

	tRGBn.r = (unsigned int) ( (float)tRGB1.r * (1-faktor) + (float)tRGB2.r * faktor);
	tRGBn.g = (unsigned int) ( (float)tRGB1.g * (1-faktor) + (float)tRGB2.g * faktor);
    tRGBn.b = (unsigned int) ( (float)tRGB1.b * (1-faktor) + (float)tRGB2.b * faktor);

	setPixelFast(x,y,tRGBn.r + tRGBn.g * 256 + tRGBn.b * 256 * 256);
  }
  else 
	setPixelFast(x,y,c);
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setBackgroundColor( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::setBackgroundColor(unsigned int c)
{
  bcolor = c;

  useBColor = true;

  return true;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setBackgroundPicture( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::setBackgroundPicture(char *f)
{
  if ( fileHandler.loadImageToPicture(f, &backPicData) == true) {
    useBColor = false;

    setSize(MainHandle->w, MainHandle->h);
    return true;
  }

  return false;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::getPixel( ... )
///////////////////////////////////////////////////////////////////////////////
unsigned int C2DScreen::getPixel(int x, int y)
{
  return d.d[y*sizeX+x];
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::getPixelRelative( ... )
///////////////////////////////////////////////////////////////////////////////
unsigned int C2DScreen::getPixelRelative(int x, int y, int relX, int relY)
{
	int pos = ((y*sizeY)/relY)*sizeX+(x*sizeX)/relX;

	return d.d[pos];
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::getRGBColor( ... )
///////////////////////////////////////////////////////////////////////////////
TRGBColorTriple C2DScreen::getRGBColor(unsigned int c)
{
  TRGBColorTriple tRGB;

  tRGB.r = c & 0xFF;
  tRGB.g = (c>>8) & 0xFF;
  tRGB.b = (c>>16) & 0xFF;

  return tRGB;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::saveBufferAsTGA( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::saveBufferAsTGA(char *f)
{
  int x,y;

  for(y=0; y<sizeY; y++)
      for(x=0; x<sizeX; x++)
		  p.rgb[y*sizeX+x] = d.d[y*sizeX+x];

  p.sizeX = sizeX;
  p.sizeY = sizeY;

  return fileHandler.saveTGAFromPicture(f, &p);
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::clear( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::clear()
{
  int x,y;

  if (useBColor)
      for(y=0; y<sizeY; y++)
          for(x=0; x<sizeX; x++)
              setPixelFast(x,y, bcolor);
  else
  {
      for(y=0; y<sizeY; y++)
		  for(x=0; x<sizeX; x++)
			  setPixelFast(x,y, backPicDataResized.rgb[y*sizeX+x]);
  }

}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setSize( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::setSize(int sX, int sY)
{
  int x,y,xo,yo;

  sizeX = sX;
  sizeY = sY;

  MainHandle->x = 0;
  MainHandle->y = 0;
  MainHandle->w = sX;
  MainHandle->h = sY;

  backPicDataResized.sizeX = sizeX;
  backPicDataResized.sizeY = sizeY;

  for(y=0; y<backPicDataResized.sizeY; y++)
	  for(x=0; x<backPicDataResized.sizeX; x++) {
		  xo = x * backPicData.sizeX/backPicDataResized.sizeX;
		  yo = y * backPicData.sizeY/backPicDataResized.sizeY;
		  backPicDataResized.rgb[y*backPicDataResized.sizeX+x] = backPicData.rgb[yo*backPicData.sizeX+xo];
	  }
  clear();
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::bilinearScrFilter( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::bilinearScrFilter()
{

  int x,y;
  unsigned int r,g,b,c;

  TBilinearMap dtMap, defMap;

  f.blFDefMapOne(&defMap);

  if (sizeX < _minFilterDiff_)
      return;
  if (sizeY < _minFilterDiff_)
      return;

  for(y=1; y<sizeY-1; y++) {

    for(x=1; x<sizeX-1; x++) {

        dtMap.d[1][0] = (float) getRGBColor(getPixel(x,y-1)).r;
        dtMap.d[0][1] = (float) getRGBColor(getPixel(x-1,y)).r;
        dtMap.d[1][1] = (float) getRGBColor(getPixel(x,y)).r;
        dtMap.d[2][1] = (float) getRGBColor(getPixel(x+1,y)).r;
        dtMap.d[1][2] = (float) getRGBColor(getPixel(x,y+1)).r;

        r = (unsigned int) f.bilinearFilter(defMap, dtMap);

        dtMap.d[1][0] = (float) getRGBColor(getPixel(x,y-1)).g;
        dtMap.d[0][1] = (float) getRGBColor(getPixel(x-1,y)).g;
        dtMap.d[1][1] = (float) getRGBColor(getPixel(x,y)).g;
        dtMap.d[2][1] = (float) getRGBColor(getPixel(x+1,y)).g;
        dtMap.d[1][2] = (float) getRGBColor(getPixel(x,y+1)).g;

        g = (unsigned int) f.bilinearFilter(defMap, dtMap);

        dtMap.d[1][0] = (float) getRGBColor(getPixel(x,y-1)).b;
        dtMap.d[0][1] = (float) getRGBColor(getPixel(x-1,y)).b;
        dtMap.d[1][1] = (float) getRGBColor(getPixel(x,y)).b;
        dtMap.d[2][1] = (float) getRGBColor(getPixel(x+1,y)).b;
        dtMap.d[1][2] = (float) getRGBColor(getPixel(x,y+1)).b;

        b = (unsigned int) f.bilinearFilter(defMap, dtMap);

        c = r + 256*g+65536*b;

        setPixel(x,y,c);

    }

  }
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawLine( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::drawLine(int x1, int y1, int x2, int y2, unsigned int c)
{
  float x, dX, y, dY;
  int xTop, yTop, xBottom, yBottom;
  int xLeft, yLeft, xRight, yRight;
  int ix, iy;
  
  if ( (abs(x1-x2)) < (abs(y1-y2)) )
  {

    if (y1 > y2)
    {
      xTop = x2;
      yTop = y2;
      xBottom = x1;
      yBottom = y1;
    }
    else
    {
      xTop = x1;
      yTop = y1;
      xBottom = x2;
      yBottom = y2;
    }
  
    x = (float)xTop;
    dX = ((float)(xBottom-xTop)) / ((float)(yBottom-yTop+1));

    for (iy=yTop; iy<=yBottom; iy++)
    {
      setPixelFast( (int)x, iy, c);
      x += dX;
    }
    
  }
  else
  {

    if (x1 > x2)
    {
      xLeft = x2;
      yLeft = y2;
      xRight = x1;
      yRight = y1;
    }
    else
    {
      xLeft = x1;
      yLeft = y1;
      xRight = x2;
      yRight = y2;
    }
  
    y = (float)yLeft;
    dY = ((float)(yRight-yLeft)) / ((float)(xRight-xLeft+1));

    for (ix=xLeft; ix<=xRight; ix++)
    {
      setPixelFast( ix, (int)y, c);
      y += dY;
    }

  }
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawSolidBox( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::drawSolidBox(int x1, int y1, int x2, int y2, unsigned int c)
{
  int x,y;

  for(y=y1; y<=y2; y++) {
  
    for(x=x1; x<=x2; x++) {

        setPixelTransparent(x,y,c);

    }
    
  }
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawBox( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::drawBox(int x1, int y1, int x2, int y2, int y3, unsigned int c1, unsigned int c2, unsigned int c3)
{
  y2 += y1;
  y3 += y1;
  drawLine(x1,y1,x2,y1,c1);
  drawLine(x1,y2,x2,y2,c1);
  drawLine(x1,y1,x1,y2,c1);
  drawLine(x2,y1,x2,y2,c1);
  drawLine(x1,y3,x2,y3,c1);
  
  drawSolidBox(x1+1,y1+1,x2-1,y3-1,c2);
  drawSolidBox(x1+1,y3+1,x2-1,y2-1,c3);
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawGUIBox( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::drawGUIBox(int x1, int y1, int x2, int y2, int y3, unsigned int c1, unsigned int c2, unsigned int c3, char *t)
{
  int ySize, yH;

  yH = y3;
  y2 += y1;
  y3 += y1;
  drawLine(x1,y1,x2,y1,c1);
  drawLine(x1,y2,x2,y2,c1);
  drawLine(x1,y1,x1,y2,c1);
  drawLine(x2,y1,x2,y2,c1);
  drawLine(x1,y3,x2,y3,c1);
  
  drawSolidBox(x1+1,y1+1,x2-1,y3-1,c2);
  drawSolidBox(x1+1,y3+1,x2-1,y2-1,c3);

  ySize = font.getLetterData(65)->sizeY;

  drawText(x1+4, y1 + ((yH-ySize)/2)+1, t);
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawGUIStdBox( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::drawGUIStdBox(int x1, int y1, int x2, int y2, int y3, char *t)
{
	int fc = fontColor;

	fontColor = _COLOR_WHITE_;
	drawGUIBox(x1, y1, x2, y2, y3, _GUI_frameColorBox_, _GUI_fillColor1Box_, _GUI_fillColor2Box_, t);
	fontColor = fc;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawPicture( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::drawPicture(int x1, int y1, TPictureData *p)
{
	for (int y=0; y<p->sizeY; y++)
		for (int x=0; x<p->sizeX; x++)
			setPixelTransparent(x1+x, y1+y, p->rgb[y*p->sizeX+x]);
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::loadFont( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::loadFont(char* fontName)
{
	fontReady = font.loadFont(fontName);

	return fontReady;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawText( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::drawText(int x, int y, char *t)
{
	int i, xp;

	if (fontReady == false)
		return false;

	xp = x;

	for (i=0; i<(int)strlen(t); i++)
	{
		if ( drawLetter(xp, y, (int)*(t+i)) == false)
			return false;
		xp += font.getLetterData((int)*(t+i))->sizeX;
	}

	return true;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawTextCentered( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::drawTextCentered(int x1, int y1, int x2, int y2, char *t)
{
    int posx = ((x2 + x1)/2) - (getTextLength(t) / 2);
    int ySize = font.getSizeY();
    int posy = ((y2 + y1)/2) - (ySize/2);

    return drawText(posx, posy, t);
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::getTextLength( ... )
///////////////////////////////////////////////////////////////////////////////
int C2DScreen::getTextLength(char *t)
{
	int i, xp;

	if (fontReady == false)
		return -1;

	xp = 0;

	for (i=0; i<(int)strlen(t); i++)
		xp += font.getLetterData((int)*(t+i))->sizeX;

	return xp;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::setFontColor( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::setFontColor(unsigned int c)
{
	fontColor = c;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::drawLetter( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::drawLetter(int x, int y, int l)
{
	TFontLetter *letter;
	
	if (l > 512)
		return false;

	if (fontReady == false)
		return false;

	letter = font.getLetterData(l);

	for (int yp=0; yp<letter->sizeY; yp++)
		for (int xp=0; xp<letter->sizeX; xp++)
			if ( letter->rgb[yp*letter->sizeX+xp] != font.getTransparentColor() )
				setPixelTransparent(x+xp, y+yp, fontColor);

	return true;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::getGUIHandle( ... )
///////////////////////////////////////////////////////////////////////////////
TGUIHandle *C2DScreen::getGUIHandle()
{
    lastAssignedHandle++;

    if (lastAssignedHandle < _GUIHandles_)
	return (&managedGUIHandles[lastAssignedHandle]);
    else
	return NULL;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::createButton( ... )
///////////////////////////////////////////////////////////////////////////////
TGUIHandle *C2DScreen::createButton(TGUIHandle *parent, int x, int y, int w, int h, char *caption)
{
    int x1p=parent->x;
    int y1p=parent->y;

    int x1 = x1p+x;
    int y1 = y1p+y;
    int x2 = x1p+x+w;
    int y2 = y1p+y+h;
    int fc = fontColor;

    if (parent == NULL)
	return NULL;

    TGUIHandle *handle = getGUIHandle();
    if (handle == NULL)
	return NULL;

    strcpy(handle->caption, caption);
    handle->x = x;
    handle->y = y;	   
    handle->w = w;	   
    handle->h = h;
    handle->type = _GUITypeButton_;
    handle->parent = parent;

    drawLine(x1,y1,x2,y1,_GUI_frameColorBox_);
    drawLine(x1,y2,x2,y2,_GUI_frameColor2Box_);
    drawLine(x1,y1,x1,y2,_GUI_frameColorBox_);
    drawLine(x2,y1,x2,y2,_GUI_frameColor2Box_);

    drawSolidBox(x1+1, y1+1, x2-1, y2-1, _GUI_fillColor3Box_);

    fontColor = _GUI_buttonTextColor_;
    drawTextCentered(x1, y1, x2, y2, caption);
    fontColor = fc;

    return handle;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::repaintButton( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::repaintButton(int h)
{
    int x1p=managedGUIHandles[h].parent->x;
    int y1p=managedGUIHandles[h].parent->y;

    int x1 = x1p+managedGUIHandles[h].x;
    int y1 = y1p+managedGUIHandles[h].y;
    int x2 = x1p+managedGUIHandles[h].x+managedGUIHandles[h].w;
    int y2 = y1p+managedGUIHandles[h].y+managedGUIHandles[h].h;
    int fc = fontColor;

    drawLine(x1,y1,x2,y1,_GUI_frameColorBox_);
    drawLine(x1,y2,x2,y2,_GUI_frameColor2Box_);
    drawLine(x1,y1,x1,y2,_GUI_frameColorBox_);
    drawLine(x2,y1,x2,y2,_GUI_frameColor2Box_);

    drawSolidBox(x1+1, y1+1, x2-1, y2-1, _GUI_fillColor3Box_);

    fontColor = _GUI_buttonTextColor_;
    drawTextCentered(x1, y1, x2, y2, managedGUIHandles[h].caption);
    fontColor = fc;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::createWindow( ... )
///////////////////////////////////////////////////////////////////////////////
TGUIHandle *C2DScreen::createWindow(TGUIHandle *parent, int x, int y, int w, int h, int h2, char *caption)
{
    int x1p=parent->x;
    int y1p=parent->y;

    int x1 = x1p+x;
    int y1 = y1p+y;
    int x2 = x1p+x+w;
    int y2 = h;
    int y3 = h2;
    int fc = fontColor;

    if (parent == NULL)
	return NULL;

    TGUIHandle *handle = getGUIHandle();
    if (handle == NULL)
	return NULL;

    strcpy(handle->caption, caption);
    handle->x = x;
    handle->y = y;	   
    handle->w = w;	   
    handle->h = h;
    handle->h2 = h2;
    handle->type = _GUITypeWindow_;
    handle->parent = parent;

    drawGUIStdBox(x1,y1,x2,y2,y3,"");

    fontColor = _COLOR_WHITE_;
    drawTextCentered(x1, y1, x2, y3, caption);
    fontColor = fc;

    return handle;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::repaintWindow( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::repaintWindow(int h)
{
    int x1p=managedGUIHandles[h].parent->x;
    int y1p=managedGUIHandles[h].parent->y;

    int x1 = x1p+managedGUIHandles[h].x;
    int y1 = y1p+managedGUIHandles[h].y;
    int x2 = x1p+managedGUIHandles[h].x+managedGUIHandles[h].w;
    int y2 = managedGUIHandles[h].h;
    int y3 = managedGUIHandles[h].h2;
    int fc = fontColor;

    drawGUIStdBox(x1,y1,x2,y2,y3,"");

    fontColor = _COLOR_WHITE_;
    drawTextCentered(x1, y1, x2, y1+y3, managedGUIHandles[h].caption);
    fontColor = fc;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::createLabel( ... )
///////////////////////////////////////////////////////////////////////////////
TGUIHandle *C2DScreen::createLabel(TGUIHandle *parent, int x, int y, int color, char *caption)
{
    int x1p=parent->x;
    int y1p=parent->y;
    int fc = fontColor;

    if (parent == NULL)
	return NULL;

    TGUIHandle *handle = getGUIHandle();
    if (handle == NULL)
	return NULL;

    strcpy(handle->caption, caption);
    handle->x = x;
    handle->y = y;	   
    handle->w = 0;	   
    handle->h = 0;
    handle->type = _GUITypeLabel_;
    handle->fontColor = color;
    handle->parent = parent;

    fontColor = color;
    drawText(x1p+x, y1p+y, caption);
    fontColor = fc;

    return handle;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::repaintLabel( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::repaintLabel(int h)
{
    int x1p=managedGUIHandles[h].parent->x;
    int y1p=managedGUIHandles[h].parent->y;

    int x1 = x1p+managedGUIHandles[h].x;
    int y1 = y1p+managedGUIHandles[h].y;

    int fc = fontColor;

    fontColor = managedGUIHandles[h].fontColor;
    drawText(x1, y1, managedGUIHandles[h].caption);
    fontColor = fc;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::isOverGUIElement( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::isOverGUIElement(TGUIHandle *h, int x, int y)
{
    if ( (x > h->parent->x + h->x) && (x < h->parent->x + h->x + h->w) &&
	     (y > h->parent->y + h->y) && (y < h->parent->y + h->y + h->h) )
	  return true;
    else
	  return false;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::isOverNoGUIElement( ... )
///////////////////////////////////////////////////////////////////////////////
bool C2DScreen::isOverNoGUIElement(int x, int y)
{
    int i;

    // i=0 unused; i=1 main
    for (i=2; i<=lastAssignedHandle; i++)
    {
	  if (isOverGUIElement(&managedGUIHandles[i], x, y))
  	    return true;
	}
	 
    return false;
}

///////////////////////////////////////////////////////////////////////////////
// C2DScreen::repaintGUIElements( ... )
///////////////////////////////////////////////////////////////////////////////
void C2DScreen::repaintGUIElements()
{
    int i;

    // i=0 unused; i=1 main
    for (i=2; i<=lastAssignedHandle; i++)
    {
  	  if (managedGUIHandles[i].visible) {
	    if (managedGUIHandles[i].type == _GUITypeButton_)
	      repaintButton(i);
	    if (managedGUIHandles[i].type == _GUITypeWindow_)
	      repaintWindow(i);
	    if (managedGUIHandles[i].type == _GUITypeLabel_)
	       repaintLabel(i);
	  }
    }
}
